# coding: utf-8
from sqlalchemy import BigInteger, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

import goodDesignImage2018Spider.dao.dbConfig as mysqlConfig

# 对象的基类
Base = mysqlConfig.get_base()
# 初始化数据库连接
engine = mysqlConfig.get_eng()
# 创建DBSession类型
DBSession = mysqlConfig.get_db_session()
metadata = Base.metadata


class CaipuStepInfo(Base):
    __tablename__ = 'caipu_step_info'
    __table_args__ = {'comment': '菜谱步骤表'}

    caipu_step_info_id = Column(BigInteger, primary_key=True, unique=True, comment='步骤主键')
    caipu_info_id = Column(BigInteger, comment='关联的菜谱id')
    caipu_step_sort = Column(Integer, comment='步骤排序')
    caipu_step_text = Column(String(500, 'utf8mb4_general_ci'), comment='步骤文字')

    @staticmethod
    def add_step_and_img_info(cp_step, step_img):
        session = DBSession()
        # 菜谱步骤
        session.add(cp_step)
        session.flush()
        step_id = cp_step.caipu_step_info_id

        if step_img is not None:
            # 步骤图
            step_img.file_biz_id = step_id
            session.add(step_img)
        session.commit()
        session.close()
        return step_id
